﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.Infrastructure.JWT;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Application.Controllers
{
    /// <summary>
    /// 角色API
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RolesController : ControllerBase
    {
        IRoleService _roleService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService"></param>
        public RolesController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult RoleAdd(RolesInputDto inputDto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            var data = _roleService.RoleAdd(inputDto);
            if (data.Count > 0)
            {
                return Ok(new ResponseModel() { Code = State.请求成功, Data = data, Message = "添加成功!" });
            }
            else
            {
                return Ok(new ResponseModel() { Code = State.请求成功, Data = data, Message = "账号已经存在,请重新输入!" });
            }
        }

        /// <summary>
        /// 角色分页显示
        /// </summary>
        [HttpGet]
        [AllowAnonymous]//允许匿名访问
        public IActionResult Show()
        {
            return Ok(_roleService.Show());
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult RoleDelete(string id)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_roleService.RoleDelete(id));
        }
        /// <summary>
        /// 给角色添加权限
        /// </summary>
        /// <param name="rolePermission"></param>
        /// <returns></returns>
        [HttpPost("addPermission")]
        public IActionResult RoleAddPermission(RoleAddPermissionDto rolePermission)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }

            int result = _roleService.AddRolePermission(rolePermission.RId, rolePermission.PId);
            if (result == 0)
            {
                return Ok(new ResponseModel() { Code = State.请求失败, Message = "添加失败" });
            }
            return Ok(new ResponseModel() { Code = State.请求成功, Message = "添加成功" });
        }
        /// <summary>
        /// 根据id查找权限
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet("{rid}")]
        public IActionResult GetPer(string rid)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "参数有误" });
            }

            else
            {
                return Ok(new ResponseModel() { Code = State.请求成功, Message = "获取成功", Data = _roleService.GetRolePermission(rid) });
            };
        }
    }
}
